package net.orange.framework.validator;

import java.util.Collection;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.validator.IObjectValidator;
import net.orange.framework.logic.IMenuProcess;
import net.orange.framework.model.Group;
import net.orange.framework.model.Menu;

/**
 * Validation : The group code can't be deleted if it had referenced by menus.
 */
public class GroupHasMenuValidator extends FrameworkValidator implements
		IObjectValidator {

	/**
	 * The target group.
	 */
	private Group group;

	/**
	 * Disabled the default contractor.
	 */
	@SuppressWarnings("unused")
	private GroupHasMenuValidator() throws Exception {

	}

	/**
	 * Constructor with group object to initialize the validation.
	 * 
	 * @param group
	 *            The target group.
	 */
	public GroupHasMenuValidator(Group group) throws Exception {
		this.group = group;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#check()
	 */
	@Override
	public boolean check() throws Exception {
		IMenuProcess process = (IMenuProcess) ProcessFactory.get(Menu.class);
		Collection collection = process.find(group);
		return (collection == null || collection.size() <= 0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#getMessage()
	 */
	@Override
	public String getMessage() {
		return group.getGroup_name() + message.get("error2_grp_03");
	}
}
